var format = function(time, format){
    var t = new Date(time);
    var tf = function(i){return (i < 10 ? '0' : '') + i};
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
        switch(a){
            case 'yyyy':
                return tf(t.getFullYear());
                break;
            case 'MM':
                return tf(t.getMonth() + 1);
                break;
            case 'mm':
                return tf(t.getMinutes());
                break;
            case 'dd':
                return tf(t.getDate());
                break;
            case 'HH':
                return tf(t.getHours());
                break;
            case 'ss':
                return tf(t.getSeconds());
                break;
        }
    })
}

//获取本月的0点0分0秒时间戳
function getCurrentMonthFirst(){
    var date=new Date();
    date.setDate(1);
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);
    return parseInt(date.getTime()/1000);
}
//获取本月的最后一天
function getCurrentMonthLast(){
    var date=new Date();
    var currentMonth=date.getMonth();
    var nextMonth=++currentMonth;
    var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
    var oneDay=1000*60*60*24;
    return new Date(nextMonthFirstDay-oneDay);//返回本月最后一天的0点0分0秒
}
//获取本月的最后一天
function getMonthLast(){
    var date=new Date();
    var currentMonth=date.getMonth();
    var nextMonth=++currentMonth;
    var nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
    return new Date(nextMonthFirstDay-1);//返回本月的23点59分59秒
}
//获取本月的0点0分0秒
function getCurrentMonth(){
    return format(getCurrentMonthFirst()*1000,'yyyy-MM-dd HH:mm:ss')
}

//返回本月最后一天的0点0分0秒
function getMonthLastDay(){
    return format(getCurrentMonthLast(),'yyyy-MM-dd HH:mm:ss')
}


//返回本月最后一天的23点59分59秒
function getMonthLastSeconds(){
    return format(getMonthLast(),'yyyy-MM-dd HH:mm:ss')
}

//获取今天的时间年月日 0点0分0秒
function today(){
    var date = new Date();
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);
    return date
}
//获取昨天的时间年月日 0点0分0秒
function yesterday(){
    var date = new Date();
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);
    var oneday = 1000 * 60 * 60 * 24;
    var yesterday = new Date(date - oneday);
    return yesterday
}

function getDate(){
    var date=new Date();
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);
    var oneday = 1000 * 60 * 60 * 24;
    //获取今天当前的时间戳0点0分0秒
    this.getToday = function(){
        return parseInt(date.getTime()/1000);
    }
    //获取昨天当前的时间戳0点0分0秒
    this.getYesterday = function(){
        var yesterday = new Date(date.getTime() - oneday);
        return  parseInt(yesterday.getTime()/1000);
    }
    //获取当前月的0点0分0秒时间戳
    this.getThisMonth = function(){
        date.setDate(1);
        return parseInt(date.getTime()/1000);
    }
    //获取当前时间戳
    this.getDangTime = function(){
        var date=new Date();
        return parseInt(date.getTime()/1000);
    }


}

//获取当前的时间
function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (hours >= 0 && hours <= 9) {
        hours = "0" + hours;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + hours + seperator2 + minutes
        + seperator2 + seconds;
    return currentdate;
}
//获取昨天的时间
function  getDayBefore(){
    var oneday = 1000 * 60 * 60 * 24;
    var date = new Date(new Date().getTime() - oneday);
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var hours = date.getHours();
    var minutes = date.getMinutes();
    var seconds = date.getSeconds();
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (hours >= 0 && hours <= 9) {
        hours = "0" + hours;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    var yesterdsay = date.getFullYear() + seperator1 + month + seperator1 + strDate
        + " " + hours + seperator2 + minutes
        + seperator2 + seconds;
    return yesterdsay;
}
//获取url的字段
function getUrl(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

//在layui中传值地址使用
function getUrlParms(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = location.search.match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}
//秒转换时分秒
function secondToDate(result) {

    var h = Math.floor(result / 3600) < 10 ? '0'+Math.floor(result / 3600) : Math.floor(result / 3600);
    var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
    var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
    return result = h + ":" + m + ":" + s;
}

var d = new getDate()
var td = d.getToday()
var yd = d.getYesterday()
var gd = d.getThisMonth()
var dd = d.getDangTime()

// console.log("dd==",dd)
// console.log("今天的0点0分0秒时间戳 td====",td)
// console.log("昨天的0点0分0秒时间戳 yd====",yd)
// console.log("本月1号的0点0分0秒时间戳 gd====",gd)
// console.log("本月1号的0点0分0秒 getCurrentMonth===",getCurrentMonth())
// console.log("当前时间 getNowFormatDate==",getNowFormatDate())
// console.log("昨天的当前时间 getDayBefore==",getDayBefore())
// console.log("本月的最后一天时间0点0分0秒 getMonthLastDay==",getMonthLastDay())
// console.log("本月的最后一天时间23点59分59秒 getMonthLastSeconds==",getMonthLastSeconds())